/*  GStruct: data structure helpers for Java
 *  Copyright (C) 2011 Giulio Franco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package gstruct.cli;

import java.util.ResourceBundle;

/**
 * Object indicating a parse error
 * @author Giulio Franco <giulio_franco@msn.com>
 *
 * @param <ResultType>	Type of the processed results
 */
public class ParseError<ResultType> {
	/**
	 * Argument that generated the error
	 */
	public final CLIArgument<? super ResultType> source;
	
	/**
	 * Format to use to print the error message
	 */
	public final String messageFormat;
	
	/**
	 * Arguments to pass to the format
	 */
	public final String[] messageArguments;
	
	/**
	 * Creates a ParseError
	 * @param source	Argument that generated the error
	 * @param message	Format to use to print the error message
	 * @param arguments Arguments to pass to the format
	 */
	public ParseError(CLIArgument<? super ResultType> source,
			String message, String... arguments) {
		
		this.source = source;
		this.messageFormat = message;
		this.messageArguments = arguments;
	}

	/**
	 * Returns the source argument
	 * @return Argument that generated the error
	 */
	public CLIArgument<? super ResultType> getSource() {
		return source;
	}

	/**
	 * Returns the message format
	 * @return Format to use to print the error message
	 */
	public String getMessage() {
		return messageFormat;
	}
	
	/**
	 * Returns an array of arguments that shall be inserted in the message
	 * @return Arguments to pass to the format
	 */
	public String[] getArguments() {
		return messageArguments.clone();
	}
	
	/**
	 * Returns a formatted and localized version of the message
	 * @param bundle	ResourceBundle for localization
	 * @return			Formatted and localized message
	 */
	public String getFormattedMessage(ResourceBundle bundle) {
		return String.format(
				bundle.getString(messageFormat), (Object[])messageArguments);
	}
}
